
<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true">
      <el-form-item>
        <!-- <el-input v-model="searchObj.name" :input="findNameByKey()" placeholder="讲师"/> -->
        <!-- <el-select
          v-model="searchObj.name"
          :remote-method="findNameByKey"
          :loading="loading"
          filterable
          remote
          reserve-keyword
          placeholder="讲师">
          <el-option
            v-for="item in names"
            :key="item.value"
            :label="item.name"
            :value="item.name"/>
        </el-select> -->

        <el-autocomplete
          v-model="searchObj.name"
          :fetch-suggestions="querySearch"
          :trigger-on-focus="false"
          class="inline-input"
          placeholder="讲师姓名"
          value-key="name"
          @select="handleSelect"
        />
      </el-form-item>

      <el-form-item>
        <el-select v-model="searchObj.level" clearable placeholder="头衔">
          <el-option value="1" label="高级讲师"/>
          <el-option value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>

      <el-form-item label="入驻时间">
        <el-date-picker
          v-model="searchObj.joinDateBegin"
          placeholder="开始时间"
          value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="-">
        <el-date-picker
          v-model="searchObj.joinDateEnd"
          placeholder="结束时间"
          value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
        <el-button type="default" @click="resetData()">清空</el-button>
      </el-form-item>
    </el-form>
    <el-button type="danger" size="medium" style="margin-bottom:20px;" icon="el-icon-delete" @click="removeRows">批量删除</el-button>
    <!-- 表格 -->
    <el-table :data="teacherList" border stripe @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"/>
      <el-table-column type="index" width="50">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称" width="80" />
      <el-table-column label="头衔" width="90">
        <template slot-scope="scope">
          <span v-if="scope.row.level === 1">
            <el-tag type="success" size="mini">高级讲师</el-tag>
          </span>
          <span v-if="scope.row.level === 2">
            <el-tag size="mini">首席讲师</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="intro" label="资历" />
      <el-table-column prop="sort" label="排序" width="60" />
      <el-table-column prop="joinDate" label="入驻时间" width="160" />
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="'/teacher/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :page-sizes="[5, 10, 15, 50]"
      :page-size="limit"
      :total="total"
      layout=" sizes, prev, pager, next, jumper,->,total"
      style="padding:30px 0;text-align:center"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"/>
  </div>
</template>
<script>
import teacherApi from '@/api/teacher'
export default {
  data() {
    return {
      teacherList: [],
      total: 0,
      page: 1,
      limit: 5,
      loading: false,
      multipleSelection: [],
      names: [],
      searchObj: {} // 查询表单
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      teacherApi.pageList(this.page, this.limit, this.searchObj).then(({ data }) => {
        this.teacherList = data.rows
        this.total = data.total
      })
    },
    handleSizeChange(limit) {
      this.limit = limit
      this.fetchData()
    },
    handleCurrentChange(page) {
      this.page = page
      this.fetchData()
    },
    resetData() {
      this.searchObj = {}
      this.fetchData()
    },
    // 删除按钮
    removeById(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        teacherApi.removeById(id).then((response) => {
          this.$message({
            message: response.message,
            type: 'success'
          })
          this.fetchData()
        })
      }).catch((err) => {
        if (err === 'cancel') {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    removeRows() {
      if (this.multipleSelection.length !== 0) {
        const idList = []
        this.multipleSelection.forEach(item => {
          idList.push(item.id)
        })
        this.$confirm('此操作将永久删除这些数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          teacherApi.removeRows(idList).then((response) => {
            this.$message({
              message: response.message,
              type: 'success'
            })
            this.fetchData()
          })
        }).catch((err) => {
          if (err === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          }
        })
      } else {
        this.$message.warning('请选择要删除的记录！')
      }
    },
    querySearch(queryString, callback) {
      if (queryString) {
        this.loading = true
        teacherApi.findNameByKey(queryString).then(({ data }) => {
          callback(data.nameList)
          // const list = []
          // data.nameList.forEach((item) => {
          //   list.push(item.name)
          //   console.log(list)
          //   callback(list)
          // })
          this.loading = false
        })
      }
    },
    handleSelect() {

    }
  }
}
</script>

<style>
</style>
